package cn.edu.zust.se.gateway.service.impl;

import cn.dev33.satoken.stp.StpInterface;
import cn.edu.zust.se.api.entity.vo.UserVo;
import cn.edu.zust.se.api.enums.RedisKeyEnum;
import cn.edu.zust.se.api.enums.UserTypeEnum;
import cn.edu.zust.se.gateway.utils.RedisUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 自定义权限验证接口扩展 
 */
@Component
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface {
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 返回此 loginId 拥有的权限列表 
        return null;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 返回此 loginId 拥有的角色列表
        UserVo user = (UserVo) RedisUtil.get(RedisKeyEnum.AUTH_CODE.getPrefix() + loginId);
        List<String> roles = new ArrayList<>();
        if (user == null) {
            return roles;
        }
        if (Objects.equals(user.getType(), UserTypeEnum.ADMIN.getCode())) {
            roles.add(UserTypeEnum.ADMIN.getMsg());
        } else if (Objects.equals(user.getType(), UserTypeEnum.USER.getCode())) {
            roles.add(UserTypeEnum.USER.getMsg());
        }
        return roles;
    }


}
